package com.example.administrator.weixintext;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //底部4个按钮
    private ImageButton image_chat;
    private ImageButton image_list;
    private ImageButton image_find;
    private ImageButton image_me;

    //4个碎片
    private ChatFragment wxChatFragment = new ChatFragment();
    private ListFragment wxListFragment = new ListFragment();
    private FindFragment wxFindfragment = new FindFragment();
    private MeFragment wxMeFragment = new MeFragment();

    private String[] data = {"apple","orage"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //顶部菜单栏屏蔽
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

        //注册底部4个按钮
        image_chat = (ImageButton) findViewById(R.id.img_chat);
        image_list = (ImageButton) findViewById(R.id.img_list);
        image_find = (ImageButton) findViewById(R.id.img_find);
        image_me = (ImageButton) findViewById(R.id.img_me);

        image_chat.setOnClickListener(this);
        image_list.setOnClickListener(this);
        image_find.setOnClickListener(this);
        image_me.setOnClickListener(this);

        replaceFragment(wxChatFragment);
    }

    @Override
    public void onClick(View v) {
        setTabDefaultColor();
        switch (v.getId()) {
            case R.id.img_chat:
                image_chat.setImageResource(R.drawable.weixin_pressed);
                replaceFragment(wxChatFragment);
                break;
            case R.id.img_list:
                image_list.setImageResource(R.drawable.contact_list_pressed);
                replaceFragment(wxListFragment);
                break;
            case R.id.img_find:
                image_find.setImageResource(R.drawable.find_pressed);
                replaceFragment(wxFindfragment);
                break;
            case R.id.img_me:
                image_me.setImageResource(R.drawable.profile_pressed);
                replaceFragment(wxMeFragment);
                break;
        }
    }

    //恢复底部菜单图片
    private void setTabDefaultColor() {
        image_chat.setImageResource(R.drawable.weixin_normal);
        image_list.setImageResource(R.drawable.contact_list_normal);
        image_find.setImageResource(R.drawable.find_normal);
        image_me.setImageResource(R.drawable.profile_normal);
    }

    //更换中间的flay_menu
    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.flay_menu, fragment);
        transaction.commit();
    }


}
